package com.ruoyi.project.manhua.webLook.service.impl;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.framework.redis.RedisKey;
import com.ruoyi.framework.redis.RedisUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.manhua.name.domain.MhName;
import com.ruoyi.project.manhua.newXml.MhNameNewMapper;
import com.ruoyi.project.manhua.webLook.entity.MhWebName;
import com.ruoyi.project.manhua.webLook.enumClass.exceptionEnum;
import com.ruoyi.project.manhua.webLook.service.MhClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
public class MhClassifyServiceImpl implements MhClassifyService {

    private static Map<String, Long> map = new HashMap<>();

    private static final String all = "quanbu";

    @Autowired
    private MhNameNewMapper mhNameNewMapper;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;


    @Autowired
    private RedisUtils redisUtils;

    /*启用redis开关*/
    @Value("${spring.redis.enabled}")
    private boolean enabled;

    static {
        map.put("rexue", 100L);
        map.put("lianai", 102L);
        map.put("gufeng", 103L);
        map.put("siaoyuan", 104L);
        map.put("qihuan", 105L);
        map.put("chunai", 106L);
        map.put("weimei", 107L);
        map.put("riman", 108L);
        map.put("hanman", 109L);
        map.put("danvzhu", 110L);
        map.put("chuanyue", 111L);
        map.put("mengxi", 112L);
        map.put("kongbu", 113L);
        map.put("xuanyi", 114L);
        map.put("xuanhuan", 115L);
        map.put("gaoxao", 116L);
        map.put("dushi", 117L);
        map.put("zhengnengliang", 118L);
        map.put("zongcai", 119L);
    }

    @Override
    public AjaxResult selectWebLimitList(Integer num, Integer page, String type) {

        Integer strip = ((num - 1) * page);
        Integer pageNum = (page);

        List<MhName> mhNames = null;

        /*是否开启redis*/
        if (enabled) {
            //拼接字符串 ：mhname-quanbu-12-2
            mhNames = (List<MhName>) redisUtils.get(RedisKey.classifyRedis.classify_data + "-" + type + "-" + strip + "-" + pageNum);
            /*是否等于null*/
            if (CollectionUtils.isEmpty(mhNames)) {
                /*防止 并发查询*/
                synchronized (this) {

                    mhNames = (List<MhName>) redisUtils.get(RedisKey.classifyRedis.classify_data + "-" + type + "-" + strip + "-" + pageNum);

                    if (CollectionUtils.isEmpty(mhNames)) {
                        /*查看是 全部查询 还是分类*/
                        mhNames = getClassIfy(type, strip, pageNum);

                        /*加入redis数据*/
                        redisUtils.set(RedisKey.classifyRedis.classify_data + "-" + type + "-" + strip + "-" + pageNum, mhNames, RedisKey.time);
                    }
                }
            }
        } else {
            mhNames = getClassIfy(type, strip, pageNum);
        }

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("lists", mhNames);
        String avatar = null;
        if (strip == 0) {
            if (ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {
                avatar = "no";
            } else {
                avatar = ShiroUtils.getSysUser().getAvatar();
            }
            ajaxResult.put("avatar", avatar);
        }

        return ajaxResult;
    }

    /**
     * 获取头像
     *
     * @return
     */
    @Override
    public String getAvatar() {
        if (ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {
            return "no";
        }
        return ShiroUtils.getSysUser().getAvatar();
    }


    private List<MhName> getClassIfy(String type, Integer strip, Integer pageNum) {
        List<MhName> mhNames = null;
        /*查看是 全部查询 还是分类*/
        if (all.equals(type)) {
            mhNames = mhNameNewMapper.seletWebLimitList(new MhWebName(strip, pageNum));

            if (mhNames.size() == 0) {

                throw new RuntimeException("别再滑了，我也是有底线的^_^..");
            }

        } else {

            /*查看是否有这个分类 否 抛出异常*/
            exceptionEnum.Checker.check(map.get(type) == null, exceptionEnum.classify_data_null);

            /*分类查询*/
            mhNames = mhNameNewMapper.seletWebLimitList(new MhWebName(map.get(type), strip, pageNum));

            if (mhNames.size() == 0 && strip.equals(0)) {
                if (!ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {
                    throw new RuntimeException("抱歉资源不足，试试其他分类吧^_^.." + "|" + ShiroUtils.getSysUser().getAvatar());
                }
                throw new RuntimeException("抱歉资源不足，试试其他分类吧^_^..");

            } else if (mhNames.size() == 0) {

                throw new RuntimeException("别再滑了，我也是有底线的^_^..");

            }
        }

        mhNames.forEach((e) -> {
            e.setTheCover(Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getTheCover())));
        });

        return mhNames;
    }


}
